<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%hj_report}}".
 *
 * @property int $id 产值ID
 * @property int $year_value_goal 年度产值目标
 * @property int $month_value_goal 本月产值目标
 * @property int $month_value_reality 本月实际产值
 * @property int $month_receipt 本月收款额
 * @property int $year_receipt 年度收款额
 * @property int $department_Id 关联部门ID
 * @property mixed project
 * @property mixed department
 */
class Report extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%report}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['department_id', 'create_at'], 'integer'],
            [['year_value_goal'], 'number'],
        ];
    }

    public function getDepartment() {
        return $this->hasOne(Department::class, ['id' => 'department_id']);
    }

    public function getProject() {
        return $this->hasMany(Project::class, ['department_id' => 'department_id']);
    }

    public function getReportMonth() {
        return $this->hasMany(ReportMonth::class, ['department_id' => 'department_id']);
    }

    /**
     * @return array|mixed
     */
    public function extraFields()
    {
        $fields['department'] = function() {
            return $this->department->name;
        };
        $fields['project'] = function() {
            return $this->project;
        };

        return $fields;
    }

    // 统计项目完成数量
    public static function completeNum($params, $status) {
        // 统计项目完成数量
        $project = Project::find()->where(['progress_status' => $status, 'department_id' => $params]);
        $start_time = strtotime(date('Y-01-01'));
        $end_time = strtotime( date('Y-12-31'));
        if ($status == 0) {
            $project->andWhere(['>=', 'created_at',$start_time]);
            $project->andWhere(['<', 'created_at',$end_time]);
        }
        if ($status == 1) {
            $project->andWhere(['>=', 'complete_at',$start_time]);
            $project->andWhere(['<', 'complete_at',$end_time]);
        }
        $project_num = $project->count();
        return $project_num;
    }

    // 获取本月新建项目
    public static function newProject($params) {
        $project = Project::find()->where(['department_id' => $params['id']]);
        $month_start = date('Y-m-01', time());
        $month_end = date('Y-m-t', time());
        $project->andWhere(['>=', 'created_at',strtotime($month_start)]);
        $project->andWhere(['<', 'created_at',strtotime($month_end)]);
        $project_new = $project->count();
        return $project_new;
    }

    // 获取本月收款额
    public static function balanceMonth($params) {
        $project = Project::find()->where(['department_id' => $params['id']]);
        $project->andWhere(['>', 'money_received',0]);
        $month_start = date('Y-m-01', time());
        $month_end = date('Y-m-t', time());
        $project->andWhere(['>=', 'created_at',strtotime($month_start)]);
        $project->andWhere(['<', 'created_at',strtotime($month_end)]);
        $month_num = $project->sum('money_received');
        return $month_num;
    }

    // 年收款额
    public static function balanceYear($params) {
        $project = Project::find()->where(['department_id' => $params['id']]);
        $start_time = strtotime(date('Y-01-01'));
        $end_time = strtotime( date('Y-12-31'));
        $project->andWhere(['>', 'money_received',0]);
        $project->andWhere(['>=', 'created_at',$start_time]);
        $project->andWhere(['<', 'created_at',$end_time]);
        $year_num = $project->sum('money_received');
        return $year_num;
    }
}
